package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.page.TableDataApi;
import com.ruoyi.yipaiju.domain.ModelConfig;
import com.ruoyi.yipaiju.service.IModelConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * 首页模块配置Controller
 * 
 * @author liuwp
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/model/config")
public class ModelConfigController extends BaseController
{
    @Autowired
    private IModelConfigService modelConfigService;

    @GetMapping("/list")
    public TableDataApi list(ModelConfig modelConfig)
    {
        startPage();
        List<ModelConfig> list = modelConfigService.selectModelConfigList(modelConfig);
        return getTableApi(list);
    }

    @GetMapping("/list/all")
    public ApiResult listAll(ModelConfig modelConfig)
    {
        List<ModelConfig> list = modelConfigService.selectModelConfigList(modelConfig);
        return ApiResult.success(list);
    }

    /**
     * 随机取一个
     * @param modelConfig
     * @return
     */
    @GetMapping("/random")
    public ApiResult random(ModelConfig modelConfig)
    {
        List<ModelConfig> list = modelConfigService.selectModelConfigList(modelConfig);
        if (list.size() == 0){
            return ApiResult.success(new ModelConfig());
        }else if (list.size() == 1){
            return ApiResult.success(list.get(0));
        }else {
            // 随机取一个
            Random random = new Random();
            int num = random.nextInt(list.size());
            return ApiResult.success(list.get(num));
        }
    }

}
